<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;

class FollowersController extends Controller{

	public function __construct(){
		$this->middleware('auth');
	}

	/**
	 * 关注
	 *
	 * @param \App\Models\User $user
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function store(\App\Models\User $user){
		$this->authorize('follow', $user);
		if(!Auth::user()->isFollowing($user->id)){
			Auth::user()->follow($user->id);
		}
		return redirect()->route('users.show', $user->id);
	}

	/**
	 * 取消关注
	 *
	 * @param \App\Models\User $user
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function destroy(\App\Models\User $user){
		$this->authorize('follow', $user);
		if(Auth::user()->isFollowing($user->id)){
			Auth::user()->unfollow($user->id);
		}
		return redirect()->route('users.show', $user->id);
	}
}
